#!/usr/bin/env python

import sys
import datetime
import hashlib
import logging
import pickle
import re
import time
import urllib
import wsgiref.handlers
import hashlib

from string import split
from urlparse import urlparse
from google.appengine.api import urlfetch
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.runtime import apiproxy_errors


class CatchReport(db.Model):
  package_version = db.TextProperty(required=True)
  package_name = db.TextProperty(required=True)
  stacktrace = db.DateTimeProperty(required=True)


class HomeHandler(webapp.RequestHandler):
  def post(self):
    # Handle the input form to redirect the user to a relative url
    package_version = self.request.get("package_version")
    package_name = self.request.get("package_name")
    stacktrace = self.request.get("stacktrace")

    logging.debug('package_version = "%s"\npackage_name = "%s"\nstacktrace = "%s"',package_version,package_name,stacktrace)

    #entry_point = CatchReport(package_version=db.Text(package_version),package_name=db.Text(package_name),stacktrace=db.Text(stacktrace))
    #try:
    #  entry_point.put()
    #except (db.Error, apiproxy_errors.Error):
    #    logging.error('Could not insert EntryPoint. Error= "%s"', db.Error())
    #    logging.debug('package_version = "%s" package_name = "%s" url = "%s", mirrored_url = "%s" cookies = "%s"', m.hexdigest(), base_url, self.request.url, mirrored_url, cookies_str

  def get(self):
      i = 1    

app = webapp.WSGIApplication([
    (r"/", HomeHandler),
    (r"/catch", HomeHandler),
], debug=True)


def main():
  wsgiref.handlers.CGIHandler().run(app)


if __name__ == "__main__":
  main()
